package nl.vu.few.ibivu.biomodels.client.ui;

import java.util.ArrayList;
import java.util.List;

import nl.vu.few.ibivu.biomodels.client.ui.view.ModelExecutionAnalysisView;
import nl.vu.few.ibivu.biomodels.shared.PetrifierModelExecution;

import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class ModelExecutionAnalysisImpl extends VLayout implements ModelExecutionAnalysisView {
	ListGrid sccGrid;
	Presenter presenter;

	public ModelExecutionAnalysisImpl() {
		super();
		sccGrid = new ListGrid();
		addMember(sccGrid);
	}
	
	private void updateExecution() {
		PetrifierModelExecution execution = presenter.getModelExecution();
		ArrayList<StateRecord> records = new ArrayList<StateRecord>();
		int sccIdx = 0;
		int stateIdx = 0;
		if (execution != null) {
			for (List<List<Integer>> scc : execution.getSccList()) {
				for (List<Integer> values : scc) {
					records.add(new StateRecord(sccIdx, stateIdx, execution.getNodeIdList(), values, false));
				}
			}
		}
		sccGrid.setData((ListGridRecord[])records.toArray());
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		updateExecution();
	}
	
	private class StateRecord extends ListGridRecord {
		public StateRecord(int scc, int state, List<String> genes, List<Integer> values, boolean terminal) {
			if (genes.size() != values.size()) {
				throw new IllegalArgumentException();
			}
			for (int i = 0; i < genes.size(); i++) {
				setAttribute(genes.get(i), values.get(i));
			}
			setAttribute("state", state);
			setAttribute("scc", scc);
			setAttribute("terminal", terminal);
		}
	}
}
